package com.breaktian.assemble.lottieanim;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.airbnb.lottie.ImageAssetDelegate;
import com.airbnb.lottie.LottieAnimationView;
import com.airbnb.lottie.LottieImageAsset;
import com.breaktian.assemble.AppGlobal;
import com.breaktian.assemble.utils.LogUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * Created by minglei on 2018/7/10.
 */

public class LottieAnimUtils {

    private static final String TAG = LottieAnimUtils.class.getSimpleName();

    public static void startLottieAnim(LottieAnimationView view, final String filePathInAsset, boolean loop) {
//        try {
//            BufferedReader bufferedReader = null;
//            StringBuilder stringBuilder = new StringBuilder();
//
//            InputStream is = AppGlobal.getContext().getAssets().open(filePathInAsset + "/data.json");
//            bufferedReader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
//            String content = null;
//            while ((content = bufferedReader.readLine()) != null) {
//                stringBuilder.append(content);
//            }
//            String animationJsonContent = stringBuilder.toString();
//            JSONObject jsonObject = new JSONObject(animationJsonContent);
//            view.setAnimation(jsonObject);
//            view.loop(loop);
//            view.playAnimation();
//        } catch (IOException e) {
//            LogUtil.e("load animation json error: " + e.getMessage());
//        } catch (JSONException e) {
//            e.printStackTrace();
//        }
//
//        view.setImageAssetDelegate(new ImageAssetDelegate() {
//            @Override
//            public Bitmap fetchBitmap(LottieImageAsset lottieImageAsset) {
//                try {
//                    TLog.i(TAG, "fetchBitmap in thread : " + Thread.currentThread().getName());
//                    InputStream inputStream =
//                            TPApplication.getAppContext().getAssets().open(filePathInAsset + "/images/" + lottieImageAsset.getFileName());
//                    return BitmapFactory.decodeStream(inputStream);  ///把流转化为Bitmap图片
//                } catch (Exception e) {
//                    TLog.e(TAG, "fetchBitmap error : " + e.getMessage());
//                    TLog.printStackTrace(e);
//                }
//                return null;
//            }
//        });
    }

    public static void startLottieAnim(LottieAnimationView view, final String filePathInAsset) {
        startLottieAnim(view, filePathInAsset, false);
    }
}
